*' --------------
* sameness effects: overtime analysis ----
*' ==============

clear all
* set up your working directory here
global home_dir ""

cd ${home_dir}

* predicted margins for interaction
cap: program drop margin_main
program margin_main
	args X k mname year vv geo_type
	sum `X', d 
	local gap = (`r(max)' - `r(min)') / `k' 

	margin if e(sample), at(`X' = (`r(min)' (`gap') `r(max)')) predict(pr) post
	
	matrix A = r(table)
	matrix B = r(at)

	mat2txt, matrix(A) saving("./output/margins/`vv'_margins_main_`mname'_`X'_`year'_`geo_type'.txt") replace
	mat2txt, matrix(B) saving("./output/margins/`vv'_at_main_`mname'_`X'_`year'_`geo_type'.txt") replace

end 

cap: program drop margin_interact
program margin_interact 
	args X Y k mname year vv geo_type
	sum `X', d 
	local gap = (`r(max)' - `r(min)') / `k' 

	margin `Y' if e(sample), at(`X' = (`r(min)' (`gap') `r(max)')) predict(pr) post
	
	matrix A = r(table)
	matrix B = r(at)

	mat2txt, matrix(A) saving("./output/margins/`vv'_margins_interact_`mname'_`X'_`Y'_`year'_`geo_type'.txt") replace
	mat2txt, matrix(B) saving("./output/margins/`vv'_at_interact_`mname'_`X'_`Y'_`year'_`geo_type'.txt") replace

end 

cap: program drop run_figure2
program run_figure2

args geo_type

use "./data/processed_notshared/cleaned_regtable_unemployment_`geo_type'_v2.dta", clear 

local vv "v1"
local year "all"

svyset [pw=weight] 	

if ("`vv'" == "v1"){
	keep if sample_v1 == 1	
}
if ("`vv'" == "v2") {
	keep if sample_v2 == 1	
}

local demographics_individual Female agegroup Race6 BornUSA  
local demographics_county RAT_Female RAT_AgeGrp4_2 RAT_AgeGrp4_3 RAT_AgeGrp4_4 RAT_Race6_2 RAT_Race6_3 RAT_Race6_4 RAT_Race6_5 RAT_Race6_6 RAT_BornUSA 
local contextual_control pop_density

egen n_miss = rowmiss(Suic St `geo_type' Year `demographics_individual' `demographics_county' `contextual_control')
recode n_miss (0=0) (1/max=1)

* for appendix table : compare emp/nonemp/notinlabor + overall
*sum Suic St `geo_type' Year `demographics_individual' `demographics_county' `contextual_control'

drop if n_miss > 0

*--------------
* Figure S6
*==============
local eq_demographics_individual Female i.agegroup i.Race6 BornUSA 
local eq_demographics_county RAT_Female RAT_AgeGrp4_2 RAT_AgeGrp4_3 RAT_AgeGrp4_4 RAT_Race6_2 RAT_Race6_3 RAT_Race6_4 RAT_Race6_5 RAT_Race6_6 RAT_BornUSA 
local eq_contextual_control pop_density

local model_eq `eq_demographics_individual' `eq_demographics_county' `eq_contextual_control' 

***************************************
* year by year sameness effects : Panel A.
***************************************
forvalues yy = 2005/2017 {
	logit Suic i.St `model_eq' std_same_prop_empstat [pw=weight], cluster(`geo_type'), if empstat == 2 & Year == `yy'
	mchange std_same_prop_empstat, amount(all)  stat(ci pvalue) decimals(7)
	matrix A = r(table)
	mat2txt, matrix(A) saving("./output/margins/v1_mchange_`yy'_unemployed_sameness_`geo_type'.txt") replace
}

***************************************
* year by year sameness effects: comparison Panel B.
***************************************
* here, focus on comparison of effect sizes
recode empstat (1=2 "Employed") (2=1 "Unemployed") (3=3 "NLF"), gen(empstat2)

forvalues yy = 2005/2017 {
	di "now running ... `yy' for pairwise comparison"
	logit Suic i.St `model_eq' b1.empstat2 c.std_same_prop_empstat b1.empstat2#c.std_same_prop_empstat [pw=weight], cluster(`geo_type'), if Year == `yy'
	margins empstat2, dydx(std_same_prop_empstat) predict(pr) pwcompare 
	matrix A = r(table_vs)
	mat2txt, matrix(A) saving("./output/margins/v1_mcompare_`yy'_unemployed_sameness_`geo_type'.txt") replace
}

end

run_figure2 cz
run_figure2 county




